package com.mycontentprovider1;

import android.app.Activity;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

/**
 * Created by 张英杰 on 2017/4/27.
 */

public class MainActivity extends Activity {
    Uri parse_uri = Uri.parse("content://com.mycontentprovider1/student");

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        findViewById(R.id.insert)
                .setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        ContentValues values = new ContentValues();
                        values.put("name", "阿三");
                        values.put("number", "10");
//                        ContentResolver contentResolver = getContentResolver();
                        getContentResolver().insert(parse_uri, values);
                    }
                });
        findViewById(R.id.search)
                .setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Cursor query = null;
                        try {
                            query = getContentResolver().query(parse_uri, null, null, null, null);
                            while (query != null && query.moveToNext()) {
                                String string = query.getString(1);
                                String string1 = query.getString(2);
                                Log.i("===", "name==" + string + "  number==" + string1);
                                Toast.makeText(MainActivity.this, "name==" + string + "  number==" + string1, Toast.LENGTH_SHORT).show();
                            }
                        } finally {
                            if (query != null) {
                                query.close();
                            }
                        }
                    }
                });
    }


}
